package com.csd.action.teacher;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;


import com.csd.helper.ActionResult;
import com.csd.helper.IConstant;
import com.csd.pojo.EvaluateItem;

import com.csd.serviceImpl.EvaluateItemService;
import com.csd.serviceImpl.ProjectService;
import com.csd.vo.TeacherTaskVO;
import com.csd.vo.UserVO;

public class TeacherHistoryAction {
	//分页
	private Integer numInPage;

	private Integer currPage;
	
	private Integer totalPage;

	public Integer getNumInPage() {
		return numInPage;
	}

	public void setNumInPage(Integer numInPage) {
		this.numInPage = numInPage;
	}

	public Integer getCurrPage() {
		return currPage;
	}

	public void setCurrPage(Integer currPage) {
		this.currPage = currPage;
	}

	public Integer getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}

	private EvaluateItemService evaluateItemService;
	private List<Integer> scores;
	private List<TeacherTaskVO> teacherHistory;
	private ProjectService projectService;

	public List<Integer> getScores() {
		return scores;
	}

	public void setScores(List<Integer> scores) {
		this.scores = scores;
	}

	public ProjectService getProjectService() {
		return projectService;
	}

	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}

	public List<TeacherTaskVO> getTeacherHistory() {
		return teacherHistory;
	}

	public void setTeacherHistory(List<TeacherTaskVO> teacherHistory) {
		this.teacherHistory = teacherHistory;
	}

	public EvaluateItemService getEvaluateItemService() {
		return evaluateItemService;
	}

	public void setEvaluateItemService(EvaluateItemService evaluateItemService) {
		this.evaluateItemService = evaluateItemService;
	}

	public String execute() {
		List<EvaluateItem> evaluateItems = null;
		String result = ActionResult.fail;

		HttpSession session = ServletActionContext.getRequest().getSession();
		UserVO userVO = (UserVO) session.getAttribute("userVO");
		Integer teacherID = null;
		teacherID = userVO.getUserID();

		evaluateItems = evaluateItemService.listTeacherEHistory(teacherID);
		if (evaluateItems != null) {
             scores = new ArrayList<Integer>();
			for (Iterator<EvaluateItem> i = evaluateItems.iterator(); i
					.hasNext();) {
                  scores.add(i.next().getGrade());
			}
		}
		
		//teacherHistory = projectService.listProjectByList(evaluateItems);
		
		List<TeacherTaskVO> old = projectService.listProjectByList(evaluateItems);
		//		对评审历史记录进行分页
		if( this.currPage == null  || this.currPage <= 0 )
			this.currPage = 1;
		
		if( this.numInPage == null || this.numInPage <= 0)
			this.numInPage = IConstant.numInPage;
		
		if(teacherHistory == null)
		{
			teacherHistory = new ArrayList<TeacherTaskVO>();
		}
		
		if(old == null)
		{
			old = new ArrayList<TeacherTaskVO>();
		}
		
		teacherHistory.clear();
		
		for(int i = (this.currPage-1)*this.numInPage; 
				i < this.currPage*this.numInPage && i < old.size(); 
				i++)
		{
			teacherHistory.add( old.get(i) );
		}
		

		this.totalPage = old.size() / this.numInPage;
		
		if(old.size() % this.numInPage != 0)
			this.totalPage++;

		result = ActionResult.success;
		return result;
	}

}
